﻿<cfscript>
/** 
* @hint "教学质量评价相关监听器"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.vote.evaluateListener" 
	output=false 
	depends="sessionAdvice, datetimeAdvice, sequenceEvaluateSystem, sequenceEvaluateTag, sequenceMeasureSet, sequenceMeasureOption" 
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	/**
	* @hint "学生评教 - 刷新教学班评论及回复进度"
	* 
	* @event "MachII Event Object"
	*/
	public void function flushEvaluateTaskComment( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 当前日期时间字符串 */
			var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
			
			/* 生成需评价教学任务的评论进度 */
			sql = "SELECT 
					a.task_id, 
					COUNT(*) comment_send, 
					SUM( CASE WHEN b.is_feed = '1' THEN 1 ELSE 0 END ) comment_reply 
					FROM mv_running_task a 
					INNER JOIN t_evaluate_comment b on a.task_id = b.task_id
					GROUP BY a.task_id 
					ORDER BY a.task_id";
			
			queryObj = new Query( datasource=application.dnsMaster );
			
			var rs_comment = queryObj.execute( sql=sql ).getResult();
			
			for ( var c=1; c LTE rs_comment.recordCount; c++ ) {
				
				sql = "UPDATE t_evaluate_task 
						SET 
							comment_send = :commentSend, 
							comment_reply = :commendReply 
						WHERE 
							task_id = :taskId ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="taskId", value=rs_comment["task_id"][c], cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="commentSend", value=rs_comment["comment_send"][c], cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="commendReply", value=rs_comment["comment_reply"][c], cfsqltype="cf_sql_integer" );
				
				queryObj.execute( sql=sql );
				
			}
			
		}
		
	}
	
	/**
	* @hint "学生评教 - 刷新开课学院教学班评论及回复进度"
	* 
	* @event "MachII Event Object"
	*/
	public void function flushInstituteEvaluateTaskComment( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";
		
		var sessionAdvice = getsessionAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 当前账户所在部门 */
			var depId = sessionAdvice.getUserProp("teacherDepartment");

			/* 当前日期时间字符串 */
			var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
			
			/* 生成需评价教学任务的评论进度 */
			sql = "SELECT 
					a.task_id, 
					COUNT(*) comment_send, 
					SUM( CASE WHEN b.is_feed = '1' THEN 1 ELSE 0 END ) comment_reply 
					FROM mv_running_task a 
					INNER JOIN t_evaluate_comment b on a.task_id = b.task_id 
					WHERE 
						a.institute_id = :depId 
					GROUP BY a.task_id 
					ORDER BY a.task_id";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="depId", value=depId, cfsqltype="cf_sql_varchar" );
			
			var rs_comment = queryObj.execute( sql=sql ).getResult();
			
			for ( var c=1; c LTE rs_comment.recordCount; c++ ) {
				
				sql = "UPDATE t_evaluate_task 
						SET 
							comment_send = :commentSend, 
							comment_reply = :commendReply 
						WHERE 
							task_id = :taskId ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="taskId", value=rs_comment["task_id"][c], cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="commentSend", value=rs_comment["comment_send"][c], cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="commendReply", value=rs_comment["comment_reply"][c], cfsqltype="cf_sql_integer" );
				
				queryObj.execute( sql=sql );
				
			}
			
		}
		
	}
	
	/**
	* @hint "学生评教 - 刷新教学班评教进度"
	* 
	* @event "MachII Event Object"
	*/
	public void function flushEvaluateTaskProgress( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 当前日期时间字符串 */
			var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
			
			/* 生成需评价教学任务的参与进度 */
			sql = "SELECT 
						a.task_id, 
						COUNT( DISTINCT b.scc_id ) evs_need, 
						SUM( CASE WHEN c.state = 'C1' OR c.state = 'C2' OR c.state = 'NO' THEN 1 ELSE 0 END ) evs_progress, 
						SUM( CASE WHEN c.state = 'CP' THEN 1 ELSE 0 END ) evs_done 
					FROM mv_running_task a 
					INNER JOIN t_student_course b ON a.task_id = b.task_id 
					INNER JOIN t_course_evaluate d ON d.cid = a.cid 
					LEFT OUTER JOIN t_student_evaluate c ON c.scc_id = b.scc_id 
					GROUP BY a.task_id 
					ORDER BY a.task_id";
			
			queryObj = new Query( datasource=application.dnsMaster );
			
			var rs_progress = queryObj.execute( sql=sql ).getResult();
			
			for ( var b=1; b LTE rs_progress.recordCount; b++ ) {
				
				sql = "UPDATE t_evaluate_task 
						SET 
							evs_need = :evsNeed, 
							evs_progress = :evsProgress, 
							evs_done = :evsDone, 
							date_lastupdate = :currentDateTime 
						WHERE 
							task_id = :taskId ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="taskId", value=rs_progress["task_id"][b], cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="evsNeed", value=rs_progress["evs_need"][b], cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="evsProgress", value=rs_progress["evs_progress"][b], cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="evsDone", value=rs_progress["evs_done"][b], cfsqltype="cf_sql_integer" );
				
				queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );
				
				queryObj.execute( sql=sql );
				
			}
			
		}
		
	}
	
	
	/**
	* @hint "学生评教 - 刷新学院教学班评教进度"
	* 
	* @event "MachII Event Object"
	*/
	public void function flushInstituteEvaluateTaskProgress( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";
		
		var sessionAdvice = getsessionAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 当前账户所在部门 */
			var depId = sessionAdvice.getUserProp("teacherDepartment");
			
			/* 当前日期时间字符串 */
			var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
			
			/* 生成需评价教学任务的参与进度 */
			sql = "SELECT 
						a.task_id, 
						COUNT( DISTINCT b.scc_id ) evs_need, 
						SUM( CASE WHEN c.state = 'C1' OR c.state = 'C2' OR c.state = 'NO' THEN 1 ELSE 0 END ) evs_progress, 
						SUM( CASE WHEN c.state = 'CP' THEN 1 ELSE 0 END ) evs_done 
					FROM mv_running_task a 
					INNER JOIN t_student_course b ON a.task_id = b.task_id 
					INNER JOIN t_course_evaluate d ON d.cid = a.cid 
					LEFT OUTER JOIN t_student_evaluate c ON c.scc_id = b.scc_id 
					WHERE 
						a.institute_id = :depId 
					GROUP BY a.task_id 
					ORDER BY a.task_id";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="depId", value=depId, cfsqltype="cf_sql_varchar" );
			
			var rs_progress = queryObj.execute( sql=sql ).getResult();
			
			for ( var b=1; b LTE rs_progress.recordCount; b++ ) {
				
				sql = "UPDATE t_evaluate_task 
						SET 
							evs_need = :evsNeed, 
							evs_progress = :evsProgress, 
							evs_done = :evsDone, 
							date_lastupdate = :currentDateTime 
						WHERE 
							task_id = :taskId ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="taskId", value=rs_progress["task_id"][b], cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="evsNeed", value=rs_progress["evs_need"][b], cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="evsProgress", value=rs_progress["evs_progress"][b], cfsqltype="cf_sql_integer" );
				queryObj.addParam( name="evsDone", value=rs_progress["evs_done"][b], cfsqltype="cf_sql_integer" );
				
				queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );
				
				queryObj.execute( sql=sql );
				
			}
			
		}
		
	}
	

	/**
	* @hint "学生评教 - 初始化教学班评教进度"
	* 
	* @event "MachII Event Object"
	*/
	public void function initEvaluateTaskProgress( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 当前日期时间字符串 */
			var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();

			/* 生成统计表 */
			sql = "SELECT 
					a.task_id, a.term_id, a.cid, a.cidx, a.task_type 
					FROM mv_running_task a 
					INNER JOIN t_course_evaluate b on b.cid = a.cid 
					WHERE 
						NOT EXISTS ( 
							SELECT 1 FROM t_evaluate_task c 
							WHERE c.task_id = a.task_id 
						)";

			queryObj = new Query( datasource=application.dnsMaster );
			
			var rs_task = queryObj.execute( sql=sql ).getResult();		
			
			for ( var a=1; a LTE rs_task.recordCount; a++ ) {
				
				sql = "INSERT INTO t_evaluate_task 
						( 
							task_id, term_id, cid, cidx, task_type, evs_need, evs_progress, evs_done, comment_send, comment_reply, date_create, date_lastupdate
						) 
						VALUES 
						(
							:taskId, :termId, :courseId, :courseIndex, :taskType, 0, 0, 0, 0, 0, :currentDateTime, :currentDateTime
						) ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="taskId", value=rs_task["task_id"][a], cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="termId", value=rs_task["term_id"][a], cfsqltype="cf_sql_char" );
				queryObj.addParam( name="courseId", value=rs_task["cid"][a], cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="courseIndex", value=rs_task["cidx"][a], cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskType", value=rs_task["task_type"][a], cfsqltype="cf_sql_char" );
				
				queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );

				queryObj.execute( sql=sql );
				
			}

		}
		
	}
	
	
	/**
	* @hint "学生评教 - 初始化开课学院教学班评教进度"
	* 
	* @event "MachII Event Object"
	*/
	public void function initInstituteEvaluateTaskProgress( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";
		
		var sessionAdvice = getsessionAdvice();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 当前账户所在部门 */
			var depId = sessionAdvice.getUserProp("teacherDepartment");
			
			/* 当前日期时间字符串 */
			var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();

			/* 生成统计表 */
			sql = "SELECT 
					a.task_id, a.term_id, a.cid, a.cidx, a.task_type 
					FROM mv_running_task a 
					INNER JOIN t_course_evaluate b on b.cid = a.cid 
					WHERE 
						a.institute_id = :depId 
						AND 
						NOT EXISTS ( 
							SELECT 1 FROM t_evaluate_task c 
							WHERE c.task_id = a.task_id 
						)";

			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="depId", value=depId, cfsqltype="cf_sql_varchar" );
			
			var rs_task = queryObj.execute( sql=sql ).getResult();		
			
			for ( var a=1; a LTE rs_task.recordCount; a++ ) {
				
				sql = "INSERT INTO t_evaluate_task 
						( 
							task_id, term_id, cid, cidx, task_type, evs_need, evs_progress, evs_done, comment_send, comment_reply, date_create, date_lastupdate
						) 
						VALUES 
						(
							:taskId, :termId, :courseId, :courseIndex, :taskType, 0, 0, 0, 0, 0, :currentDateTime, :currentDateTime
						) ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				
				queryObj.addParam( name="taskId", value=rs_task["task_id"][a], cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="termId", value=rs_task["term_id"][a], cfsqltype="cf_sql_char" );
				queryObj.addParam( name="courseId", value=rs_task["cid"][a], cfsqltype="cf_sql_varchar" );
				queryObj.addParam( name="courseIndex", value=rs_task["cidx"][a], cfsqltype="cf_sql_char" );
				queryObj.addParam( name="taskType", value=rs_task["task_type"][a], cfsqltype="cf_sql_char" );
				
				queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );

				queryObj.execute( sql=sql );
				
			}

		}
		
	}

	/**
	* @hint "学生评教 - 更新学生评教执行状态"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateEvaluateResponseState( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";

		/* 选课课号 */
		var chooserId = event.getArg("SCC");
		/* 评教模式 */
		var evsMode = event.getArg("Mode");
		/* 评教阶段 */
		var stage = event.getArg("Stage");

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 当前日期时间字符串 */
			var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
			
			/* 默认未评教 */
			var state = "U";

			switch ( evsMode ) {
				
				case "M2":
					
					/* 两段式评教 */
					if ( stage eq "1" ) {
						state = "C1";
					}
					if ( stage eq "2" ) {
						state = "CP";
					}
					break;
				
				case "M3":
					
					/* 三阶段评教 */
					if ( stage eq "1" ) {
						state = "C1";
					}
					if ( stage eq "2" ) {
						state = "C2";
					}
					if ( stage eq "3" ) {
						state = "CP";
					}
					break;
			}
			
			sql = "update t_student_evaluate 
					SET 
						state = :state, 
						date_lastupdate = :currentDateTime 
					WHERE scc_id = :chooserId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="state", value=state, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );

			queryObj.execute( sql=sql );
			
			sql = "update t_target_response 
					SET 
						locked = '1', 
						date_lastupdate = :currentDateTime 
					WHERE scc_id = :chooserId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );

			queryObj.execute( sql=sql );
			
		}
		
	}
	
	/**
	* @hint "指标体系 - 删除评分参考选项"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteOptionSet( required MachII.framework.Event event ) output=false {
		
		var sql = "";
		var queryObj = "";
		
		var measureSetId = event.getArg("MSET");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "DELETE FROM t_measure_set 
					WHERE m_id = :measureSetId ";

			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="measureSetId", value=measureSetId, cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );
			
		}
		
	}

	
	/**
	* @hint "指标体系 - 创建评分参考选项"
	* 
	* @event "MachII Event Object"
	*/
	public void function createOptionSet( required MachII.framework.Event event ) output=false {
		
		var sequenceMeasureSet = getsequenceMeasureSet();
		var sequenceMeasureOption = getsequenceMeasureOption();
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 评判模式 */
			var measure = event.getArg("Measure");
			/* 计分算法 */
			var algoModel = event.getArg("AlgoModel");
			
			/* 选项数量 */
			var optionCount = event.getArg("optCount");
			
			
			/* 生成选项列表 */
			var options = "";
			
			for ( var i=1; i LTE optionCount; i++ ) {
				options = listAppend( options, event.getArg("OptionName" & i), "、" );
			}
			
			/* 百分制计分 特殊处理 */
			if ( measure eq "M" ) {
				options = "百分制分数";
			}
			
			/* 生成参考集合主键 */
			sequenceMeasureSet.setMeasure( measure );
			
			var measureSetId = sequenceMeasureSet.getID();
			

			sql = "INSERT INTO t_measure_set 
					( m_id, measure, algorithm, options ) 
					VALUES 
					( :measureSetId, :measure, :algoModel, :options ) ";

			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="measureSetId", value=measureSetId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="measure", value=measure, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="algoModel", value=algoModel, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="options", value=options, cfsqltype="cf_sql_varchar" );

			queryObj.execute( sql=sql );
			
			
			/* 生成选项 */
			if ( measure eq "L" ) {
				
				for ( var i=1; i LTE optionCount; i++ ) {

					/* 生成选项主键 */
					sequenceMeasureOption.setMeasureSet( measureSetId );
					
					var optionId = sequenceMeasureOption.getID();
					
					/* 选项序号 */
					var optionIdx = i;
					/* 选项名称 */
					var optionName = event.getArg("OptionName" & i);
					/* 选项权重 */
					var optionWeight = event.getArg("OptionWeight" & i);
					
					sql = "INSERT INTO t_measure_options 
							( opt_id, m_id, opt_idx, opt_weight, opt_name ) 
							VALUES 
							( :optionId, :measureSetId, :optionIdx, :optionWeight, :optionName ) ";
					
					queryObj = new Query( datasource=application.dnsMaster );
			
					queryObj.addParam( name="optionId", value=optionId, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="measureSetId", value=measureSetId, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="optionIdx", value=optionIdx, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="optionWeight", value=optionWeight, cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="optionName", value=optionName, cfsqltype="cf_sql_varchar" );

					queryObj.execute( sql=sql );
					
				}
				
			}
			
			
		}
		
	}
	

	/**
	* @hint "指标体系 - 批量关联课程"
	* 
	* @event "MachII Event Object"
	*/
	public void function batchAttachCourse( required MachII.framework.Event event ) output=false {
		
		var sessionAdvice = getsessionAdvice();
		
		var evsId = event.getArg("ES");

		var courseCodes = listToArray(event.getArg("CourseCodes"), ", " & chr(10) & chr(13));
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 当前日期时间字符串 */
			var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
			
			/* 创建人 */
			var createrId = sessionAdvice.getAutherUserID();
			
			for ( var courseCode in courseCodes ) {
				
				sql = "SELECT cid FROM t_course WHERE cid = :courseCode ";
				
				queryObj = new Query( datasource=application.dnsMaster );
				queryObj.addParam( name="courseCode", value=courseCode, cfsqltype="cf_sql_varchar" );
			
				var rs_course = queryObj.execute( sql=sql ).getResult();
				
				if ( rs_course.recordCount ) {
					
					var courseId = rs_course.cid;
					
					/* 若指定课程号存在 则开始检查课程关联 */
					
					sql = "SELECT 1 FROM t_course_evaluate WHERE cid = :courseId ";
					
					queryObj = new Query( datasource=application.dnsMaster );
					queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
					
					var rs_check = queryObj.execute( sql=sql ).getResult();
					
					if ( rs_check.recordCount ) {
						/* 若已关联 则更新评价体系 和 最后修改时间 更正人 */
						
						sql = "UPDATE t_course_evaluate 
								SET 
									es_id = :evsId, 
									tch_id = :createrId, 
									date_lastupdate = :currentDateTime 
								WHERE 
									cid = :courseId ";
						
						queryObj = new Query( datasource=application.dnsMaster );
						queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="evsId", value=evsId, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="createrId", value=createrId, cfsqltype="cf_sql_varchar" );
					
						queryObj.execute( sql=sql );
						
					}
					else {
						/* 创建新关联 */
						
						sql = "INSERT INTO t_course_evaluate 
								( cid, es_id, tch_id, date_create, date_lastupdate ) 
								VALUES 
								( :courseId, :evsId, :createrId, :currentDateTime, :currentDateTime ) ";
						
						queryObj = new Query( datasource=application.dnsMaster );
						queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
						queryObj.addParam( name="evsId", value=evsId, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );
						queryObj.addParam( name="createrId", value=createrId, cfsqltype="cf_sql_varchar" );
					
						queryObj.execute( sql=sql );
						
					}

				}
				
			}
			
		}
		
		/* Pass Args */
		event.setArg("ES", evsId);
		
	}
	
	/**
	* @hint "指标体系 - 删除关联课程"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteAttachCourse( required MachII.framework.Event event ) output=false {
		
		var evsId = event.getArg("ES");
		var courseId = event.getArg("CID");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "DELETE FROM t_course_evaluate WHERE cid = :courseId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );

			queryObj.execute( sql=sql );
			
		}
		
		/* Pass Args */
		event.setArg("ES", evsId);
		
	}

	
	/**
	* @hint "指标体系 - 观察点调整前后顺序"
	* 
	* @event "MachII Event Object"
	*/
	public void function swapTag( required MachII.framework.Event event ) output=false {
		
		var evsId = event.getArg("ES");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			var tagId1 = event.getArg("TAG");
			var tagId2 = event.getArg("TAG_NEXT");
			
			sql = "SELECT tag_order FROM t_target WHERE tag_id = :tagId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="tagId", value=tagId1, cfsqltype="cf_sql_char" );
			
			var rs1 = queryObj.execute( sql=sql ).getResult();
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="tagId", value=tagId2, cfsqltype="cf_sql_char" );
			
			var rs2 = queryObj.execute( sql=sql ).getResult();
			
			sql = "UPDATE t_target SET tag_order = :tagIdx WHERE tag_id = :tagId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="tagId", value=tagId1, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagIdx", value=rs2.tag_order, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
			
			queryObj = new Query( datasource=application.dnsMaster );
			queryObj.addParam( name="tagId", value=tagId2, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagIdx", value=rs1.tag_order, cfsqltype="cf_sql_char" );
			
			queryObj.execute( sql=sql );
			
		}
		
		/* Pass Args */
		event.setArg("ES", evsId);
		
	}
	
	
	/**
	* @hint "删除指标体系 - 观察点"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteTag( required MachII.framework.Event event ) output=false {
		
		var evsId = event.getArg("ES");
		var tagId = event.getArg("TAG");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "DELETE FROM t_target WHERE tag_id = :tagId ";

			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="tagId", value=tagId, cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );
			
		}
		
		/* Pass Args */
		event.setArg("ES", evsId);
		
	}
	
	/**
	* @hint "更新指标体系 - 观察点"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateTag( required MachII.framework.Event event ) output=false {
		
		var sequenceTag = getsequenceEvaluateTag();
		
		var evsId = event.getArg("ES");
		var parentOrder = event.getArg("PTO");
		var tagId = event.getArg("TAG");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
		
			/* 生成观察点 排序号 */
			sequenceTag.setEvaluateSystemID( evsId );
			sequenceTag.setParentOrder( parentOrder );
			
			var tagIdx = sequenceTag.getTagOrder();
			
			/* 观测点名称 */
			var tagTitle = event.getArg("Question");
			
			/* 观测点权重 */
			var tagWeight = event.getArg("Weight", 10);
			
			/* 观测点回答 */
			var answerId = event.getArg("Measure");
			
			/* 一级指标归属阶段 */
			sql = "SELECT level_order FROM t_target 
					WHERE es_id = :evsId AND tag_order = :parentOrder ";
			
			queryObj = new Query( datasource=application.dnsMaster );

			queryObj.addParam( name="evsId", value=evsId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="parentOrder", value=parentOrder, cfsqltype="cf_sql_char" );

			rs_parent = queryObj.execute( sql=sql ).getResult();
			
			var tagLevelOrder = rs_parent.level_order;
			
			sql = "UPDATE t_target 
					SET 
						level_order = :tagLevelOrder, 
						tag_order = :tagIdx, 
						tag_weight = :tagWeight, 
						question = :tagTitle, 
						m_id = :answerId 
					WHERE 
						tag_id = :tagId ";

			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="tagId", value=tagId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagLevelOrder", value=tagLevelOrder, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagIdx", value=tagIdx, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagTitle", value=tagTitle, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="tagWeight", value=tagWeight, cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="answerId", value=answerId, cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );
			
		}
		
		/* Pass Args */
		event.setArg("ES", evsId);
		
	}

	/**
	* @hint "建立指标体系 - 观察点"
	* 
	* @event "MachII Event Object"
	*/
	public void function createTag( required MachII.framework.Event event ) output=false {
		
		var sequenceTag = getsequenceEvaluateTag();
		
		var evsId = event.getArg("ES");
		var parentOrder = event.getArg("PTO");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 生成观察点 主键 和 排序号 */
			sequenceTag.setEvaluateSystemID( evsId );
			sequenceTag.setParentOrder( parentOrder );
			
			var tagId = sequenceTag.getID();
			var tagIdx = sequenceTag.getTagOrder();
			
			/* 一级指标归属阶段 */
			var tagLevelOrder = event.getArg("LO");
			/* 观测点名称 */
			var tagTitle = event.getArg("Question");
			/* 观测点权重 */
			var tagWeight = event.getArg("Weight", 10);
			/* 观测点回答 */
			var answerId = event.getArg("Measure");

			sql = "INSERT INTO t_target 
					( tag_id, es_id, level_order, tag_order, tag_weight, question, m_id ) 
					VALUES 
					( :tagId, :evsId, :tagLevelOrder, :tagIdx, :tagWeight, :tagTitle, :answerId ) ";

			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="tagId", value=tagId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="evsId", value=evsId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagLevelOrder", value=tagLevelOrder, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagIdx", value=tagIdx, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagTitle", value=tagTitle, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="tagWeight", value=tagWeight, cfsqltype="cf_sql_integer" );
			queryObj.addParam( name="answerId", value=answerId, cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );
			
		}
		
		/* Pass Args */
		event.setArg("ES", evsId);
		
	}

	/**
	* @hint "指标体系 - 分类指标 - 删除"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteLevelTag( required MachII.framework.Event event ) output=false {
		
		var evsId = event.getArg("ES");
		var parentOrder = event.getArg("PTO");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "DELETE FROM t_target 
					WHERE 
						es_id = :evsId 
						AND 
						tag_order LIKE :condition ";

			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="evsId", value=evsId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="condition", value=left(parentOrder,2) & "__", cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );
			
		}
		
		/* Pass Args */
		event.setArg("ES", evsId);
		
	}

	/**
	* @hint "建立指标体系 - 分类指标"
	* 
	* @event "MachII Event Object"
	*/
	public void function createLevelTag( required MachII.framework.Event event ) output=false {
		
		var sequenceTag = getsequenceEvaluateTag();
		
		var evsId = event.getArg("ES");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {

			/* 生成观察点 主键 和 排序号 */
			sequenceTag.setEvaluateSystemID( evsId );

			var tagId = sequenceTag.getID();
			var tagIdx = sequenceTag.getLevelOrder();

			/* 一级指标归属阶段 */
			var tagLevelOrder = event.getArg("LO");
			/* 一级指标名称 */
			var tagTitle = event.getArg("LevelTitle");
			/* 一级指标权重 */
			var tagWeight = event.getArg("Weight", 10);

			sql = "INSERT INTO t_target 
					( tag_id, es_id, level_order, tag_order, tag_weight, question, m_id ) 
					VALUES 
					( :tagId, :evsId, :tagLevelOrder, :tagIdx, :tagWeight, :tagTitle, 'NOP' ) ";

			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="tagId", value=tagId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="evsId", value=evsId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagLevelOrder", value=tagLevelOrder, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagIdx", value=tagIdx, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="tagTitle", value=tagTitle, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="tagWeight", value=tagWeight, cfsqltype="cf_sql_integer" );

			queryObj.execute( sql=sql );

		}

		/* Pass Args */
		event.setArg("ES", evsId);
	}
	
	
	/**
	* @hint "删除评价指标体系"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteEvaluate( required MachII.framework.Event event ) output=false {
		
		var evsId = event.getArg("ES");
		
		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "DELETE FROM t_evaluate_system WHERE es_id = :evsId ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="evsId", value=evsId, cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );
			
		}
		
	}

	/**
	* @hint "建立评价指标体系"
	* 
	* @event "MachII Event Object"
	*/
	public void function createEvaluate( required MachII.framework.Event event ) output=false {
		
		var sessionAdvice = getsessionAdvice();
		var sequenceEvS = getsequenceEvaluateSystem();

		var sql = "";
		var queryObj = "";
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			/* 当前日期时间字符串 */
			var currentDateTime = getdatetimeAdvice().getCurrentDatetimeString();
			
			/* 创建人 */
			var createrId = sessionAdvice.getAutherUserID();
			
			/* 生成评价体系主键 */
			var evsId = sequenceEvS.getID();
			/* 评价体系名称 */
			var evsName = event.getArg("EvsTitle");
			/* 评价模式 */
			var evsModel = event.getArg("EvsModel");
			/* 评价指标层次 */
			var evsLevel = event.getArg("EvsLevel");
			
			sql = "INSERT INTO t_evaluate_system 
					( es_id, es_name, es_model, es_level, tch_id, date_create, date_lastupdate ) 
					VALUES 
					( :evsId, :evsName, :evsModel, :evsLevel, :createrId, :currentDateTime, :currentDateTime ) ";
			
			queryObj = new Query( datasource=application.dnsMaster );
			
			queryObj.addParam( name="evsId", value=evsId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="evsName", value=evsName, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="evsModel", value=evsModel, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="evsLevel", value=evsLevel, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="createrId", value=createrId, cfsqltype="cf_sql_char" );
			queryObj.addParam( name="currentDateTime", value=currentDateTime, cfsqltype="cf_sql_char" );

			queryObj.execute( sql=sql );

		}

		/* Pass Args */
		event.setArg("ES", evsId);

	}

}
</cfscript>